<?php

class admin_controller_content_article extends admin_controller
{
    
	public function listing()
	{
	    $order_by = request('order_by', 'id');
		$order_by_dir = request('order_by_dir', 'ASC');
		
		$category_id = request('category_id', -1, 'int');
		$key = request('key', '');
		$status = request('status', -1);
		
		$model_article = $this->get_admin_model();
		$template = $this->get_admin_template();
        $template->set_title('文章列表');

		$limit = 20;
		$pagination = bone::get_ui('pagination');
		$pagination->set_limit( $limit );
		$pagination->set_total($model_article->get_article_sum($category_id, $key, $status));
		$pagination->set_page( get('page', 1, 'int') );
		$pagination->update();

		$template->set('pagination', $pagination);
		$template->set('order_by', $order_by);
		$template->set('order_by_dir', $order_by_dir);
		$template->set('category_id', $category_id);
		$template->set('key', $key);
		$template->set('status', $status);
		
		$template->set('articles', $model_article->get_articles($category_id, $key, $status, $order_by, $order_by_dir, $pagination->get_offset(), $limit) );
		
		$model_category = $this->get_model('category');
		$template->set('categories', $model_category->get_categories());

		$template->display();
	}
	
	
    public function edit()
	{
		$id = get('id', 0, 'int');
        
		$table_article = $this->get_table('article');
		$table_article->load($id);

		$template = $this->get_admin_template();
		if($id==0)
			$template->set_title('添加文章');
		else
			$template->set_title('编辑文章');

		$template->set('article', $table_article);
		
		$model_category = $this->get_model('category');
		$categories = $model_category->get_categories();
		$template->set('categories', $categories);
		
		$return = get('return', null);
		if($return===null) $return = base64_encode($_SERVER['HTTP_REFERER']);
		$template->set('return', $return );

		$template->display();
	}
	


	public function edit_save()
	{
		$id = post('id', 0, 'int');
		$my = bone::get_admin();

		$table_article = $this->get_table('article');
		
		$is_new = ($id==0);
		if( $id )
		{
			$table_article->load( $id );
			$table_article->modify_time = time();
			$table_article->modify_by_id = $my->id;
			$msg = '修改文章成功';
			system_log('修改文章: '.$table_article->title);
		}
		else
		{
			$table_article->create_time = time();
			$table_article->create_by_id = $my->id;
			$msg = '添加文章成功';
			system_log('添加文章: '.$table_article->title);
		}
		$table_article->bind( post() );
		$table_article->save();
		
		if($is_new)
		{
		    $table_article->ordering = $table_article->id;
		    $table_article->save();
		}

        $return = post('return');
		$this->redirect(base64_decode($return), $msg);
	}
	
	
	public function unblock()
	{
        $id = get('id');

        
        $model = $this->get_admin_model();
        
        if($model->unblock($id))
        {
            $this->set_message('公开文章成功');
            system_log('公开文章 #'.$id);
        }
        else
            $this->set_message( $model->get_error(), 'error' );

        $this->redirect($_SERVER['HTTP_REFERER']);
	}
	
	public function block()
	{
        $id = get('id');

        $model = $this->get_admin_model();
        if($model->block($id))
        {
            $this->set_message('屏蔽文章成功');
            system_log('屏蔽文章 #'.$id);
        }
        else
            $this->set_message( $model->get_error(), 'error' );

	    $this->redirect($_SERVER['HTTP_REFERER']);
	}
	
	public function delete()
	{
        $id = get('id');

        $model = $this->get_admin_model();
        if($model->delete($id))
        {
            $this->set_message('删除文章成功');
            system_log('删除文章 #'.$id);
        }
        else
            $this->set_message( $model->get_error(), 'error' );

        $this->redirect($_SERVER['HTTP_REFERER']);
	}
	


	public function ajax_set_home()
	{

		$id = get('id', 0, 'int');
		if( $id==0 )
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{id}丢失');
		}
		else
		{
			$model_article = $this->get_admin_model('article');
			if( $model_article->set_home( $id ) )
			{
				$this->set_ajax('status', 0);
				$this->set_ajax('description', '设置成功');
				system_log('设置 #'.$id.' 文章为首页');
			}
			else
			{
				$this->set_ajax('status', 2);
				$this->set_ajax('description', $model_article->get_error());
			}
		}
		$this->ajax();
	}
	
	
}
?>